package com.alex.acks;

import java.time.Duration;
import java.util.Iterator;
import java.util.Properties;
import java.util.regex.Pattern;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

/**
 * @ClassName KafkaCoumerInterceptor
 * @Description TODO
 * @Author zjg
 * @Date 2021/12/615:37
 * @Version 2.0
 */

public class KafkaCoumerAcks {

  public static void main(String[] args) {
    //创建KafkaConsumer
    Properties props = new Properties();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"49.233.172.9:9092");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
    props.put(ConsumerConfig.GROUP_ID_CONFIG,"g1");
    KafkaConsumer consumer = new KafkaConsumer<String,String>(props);
    //2、订阅相关的topic
    consumer.subscribe(Pattern.compile("^topic.*"));

    while (true){
      ConsumerRecords<String,String> consumerRecords= consumer.poll(Duration.ofSeconds(1));
      if(!consumerRecords.isEmpty()){
        Iterator<ConsumerRecord<String, String>> iterator = consumerRecords.iterator();
        while(iterator.hasNext()){
          ConsumerRecord<String, String> record = iterator.next();
          String topic = record.topic();
          int partition = record.partition();
          String key = record.key();
          String value = record.value();
          long offset = record.offset();
          long timestamp = record.timestamp();
          System.out.println(topic+"\t"+partition+"\t"+offset+"\t"+key+"\t"+value+"\t"+timestamp);
        }
      }
    }

  }
}
